package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Latentclientplan;
import cn.wolfcode.crm.mapper.LatentClientPlanMapper;
import cn.wolfcode.crm.query.LatentClientPlanQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ILatentClientPlaService;
import cn.wolfcode.crm.service.ILatentClientService;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ILatentClientPlaServiceImpl implements ILatentClientPlaService {
    @Autowired
    private LatentClientPlanMapper planMapper;

    @Autowired
    private ILatentClientService clientService;
    @Override
    public void insert(Latentclientplan record) {
        //把当前用户作为录入人
        record.setInputUser(UserContext.getCurrentUser());
        planMapper.insert(record);
    }

    @Override
    public Latentclientplan selectByPrimaryKey(Long id) {
        return planMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Latentclientplan> selectAll() {
        return planMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Latentclientplan record) {
        planMapper.updateByPrimaryKey(record);
    }


    public PageResult query(LatentClientPlanQueryObject qo) {
        int total = planMapper.queryForCount(qo);
        if (total > 0) {
            List<Latentclientplan> rows = planMapper.queryForList(qo);
            return new PageResult(rows, total);
        }
        return new PageResult();
    }

    @Override
    public void delete(Long id) {
        planMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void changeResource(Long id) {
        //拿到开计划对象
        Latentclientplan plan = planMapper.selectByPrimaryKey(id);
        //取出plan中的客户id
        Long clientId = plan.getLatentclient().getId();
        //调用方法改变客户状态
        clientService.changStatus(clientId);
        //删除当前记录
        planMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void changeState(Long id) {
        //拿到开计划对象
        Latentclientplan plan = planMapper.selectByPrimaryKey(id);
        //取出plan中的客户id
       Long clientId = plan.getLatentclient().getId();
        //调用方法改变客户状态
        clientService.turnState(clientId);
        //删除当前记录
        planMapper.deleteByPrimaryKey(id);

    }
}
